﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace SLGameEngine
{
    public class ObjectTerrain : ObjectBase
    {
        private Image[] _layers;
        private int[] _baseObjectIDs;
        private int[] _opacityIDs;
        private double _radius = 0;

        public ObjectTerrain(double x, double y, int radius, int layerCount, int baseObjID)
            : base(x, y, baseObjID)
        {
            ObjectCanvas.Children.Clear();
            _radius = radius;
            SelectionRect.Width = _radius;
            SelectionRect.Height = _radius;
            ParentCanvas.Width = _radius;
            ParentCanvas.Height = _radius;

            _baseObjectIDs = new int[layerCount];
            _opacityIDs = new int[layerCount];
            _layers = new Image[layerCount];
            for (int i = 0; i < layerCount; i++)
            {
                _layers[i] = new Image();
                ObjectCanvas.Children.Add(_layers[i]);
                _baseObjectIDs[i] = Consts.None;
                _opacityIDs[i] = Consts.None;
            }

            if (Consts.ShowGrid)
            {
                SelectionRect.Visibility = Visibility.Visible;
                SelectionRect.Stroke = new SolidColorBrush(Colors.Yellow);
            }
        }

        public void ClearLayers()
        {
            for (int i = 0; i < _layers.Length; i++)
            {
                ObjectCanvas.Children.Remove(_layers[i]);
                _layers[i] = new Image();
                ObjectCanvas.Children.Add(_layers[i]);
                _baseObjectIDs[i] = Consts.None;
                _opacityIDs[i] = Consts.None;
            }
        }

        public Image GetLayerImage(int layer)
        {
            return _layers[layer];
        }

        public int GetLayerBaseObjectID(int layer)
        {
            return _baseObjectIDs[layer];
        }

        public int GetLayerOpacityID(int layer)
        {
            return _opacityIDs[layer];
        }

        public void ApplyOpacityMask(int layer, int opacityID, Image opacityImage)
        {
            _opacityIDs[layer] = opacityID;

            ImageBrush imgBrush = new ImageBrush();
            imgBrush.ImageSource = Utils.LoadImage(Utils.GetImageFileName(opacityImage));

            _layers[layer].OpacityMask = imgBrush;
        }

        public void SetLayer(int layer, int baseObjectID, string fileName)
        {
            if (string.Empty != fileName)
            {
                _layers[layer].Visibility = Visibility.Collapsed;
                _layers[layer].Source = Utils.LoadImage(fileName);
                _layers[layer].ImageOpened += new EventHandler<RoutedEventArgs>(Image_ImageOpened);
            }
            _baseObjectIDs[layer] = baseObjectID;
        }

        private void LoadImage(string fileName)
        {
            Image img = new Image();
            Uri uri = new Uri(fileName, UriKind.Relative);
            img.Source = new System.Windows.Media.Imaging.BitmapImage(uri);
            img.ImageOpened += new EventHandler<RoutedEventArgs>(Image_ImageOpened);
        }

        void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            Image img = (Image)sender;
            if (_radius != 0)
            {
                BitmapImage bi = (BitmapImage)img.Source;
                if (bi.PixelWidth != _radius)
                    img.Height = _radius;
                if (bi.PixelWidth != _radius)
                    img.Width = _radius;
            }
            img.Visibility = Visibility.Visible;
        }

        protected override void CopyToClone(int maxRadius, ref ObjectBase clone)
        {
            for (int i = 0; i < _layers.Length; i++)
            {
                if (_layers[i] != null && _layers[i].Source != null)
                    ((ObjectTerrain)clone).SetLayer(i, _baseObjectIDs[i], ((BitmapImage)_layers[i].Source).UriSource.OriginalString);
            }

            base.CopyToClone(maxRadius, ref clone);
        }

        public override ObjectBase Clone(int maxRadius)
        {
            ObjectBase clone = (ObjectBase) new ObjectTerrain(0, 0, maxRadius, _layers.Length, BaseObjectID);

            CopyToClone(maxRadius, ref clone);
            return (ObjectBase)clone;
        }

        public override void Deselect()
        {
            if (Consts.ShowGrid)
            {
                SelectionRect.Visibility = Visibility.Visible;
                SelectionRect.Stroke = new SolidColorBrush(Colors.Yellow);
            }
            else
                SelectionRect.Visibility = Visibility.Collapsed;
        }
        public override ObjectBase Select()
        {
            SelectionRect.Stroke = new SolidColorBrush(Colors.Red);
            SelectionRect.Visibility = Visibility.Visible;
            return (ObjectBase)this;
        }

    }
}
